import {getRouterId} from "@/utils/storage";
import mainRouter from '@/routes/mainRouter';

const state = {
  routes: []
};
const mutations = {
  SET_ROUTES(state, payload) {
    state.routes = payload;
  }
};

function filterAsyncRoutes(mainRouter, router_id) {
  return mainRouter.filter(item => item.meta)
    .filter(item => item.meta.roles.indexOf(router_id) !== -1);
}

const actions = {
  generatorRoutes({commit}) {
    //筛选的过程
    // 获取所有的路由配置项里面的数组
    // 获取 router_id
    // 筛选  角色相匹配的
    // push到 routes


    return new Promise((resolve, reject) => {
      const router_id = getRouterId();
      let filterRoutes = filterAsyncRoutes(mainRouter, router_id);
      commit('SET_ROUTES', filterRoutes);
      resolve(filterRoutes);
    })
  }
};


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
